import React, {PureComponent} from 'react';
import routes from './routes';
import {Table, Card} from 'antd';
import PropTypes from 'prop-types';
import {Link} from 'react-router-dom';
import {
    PAGE,
    TABLE,
    DATETIME_FORMAT,
    PAGINATION_OPTIONS,
    COMMON_TABLE_QUERY_FAIL_TEXT,
    COMMON_TABLE_EMPTY_TEXT,
    FIXED_COLUMN_WIDTH
} from '../constants';
import {PERMISSION} from './constants';
import {formatDateTime, conventEnumValueToString} from '../utils';
import {conventSorter, hasPermissions, ListTypeCL} from './utils';
import {vehicleStatus, productGroupType} from '../Enum';
import DropdownMenu from 'Shared/components/DropdownMenu';
export class TablePanel extends PureComponent {
    state = {
        loading: false
    };
    onChangeSort = (pagination, filters, sorter) => {
        if(pagination.current - 1 === this.props.pageIndex && pagination.pageSize === this.props.pageSize)
            this.props.onChangeSort({...conventSorter(sorter),
                pageIndex: PAGE.index});
    };

    render() {
        const {formatMessage} = this.props.intl;
        const {brands} = this.props.initEnumData;
        const {pageIndex, pageSize, total, permissions} = this.props;
        const columns = [{
            title: formatMessage({
                id: 'tablePanel.column.vin',
                defaultMessage: 'VIN'
            }),
            dataIndex: 'vin',
            sorter: true,
            render: (text, record) => (
                <Link key="detail" to={routes.detail.format(record.id)}>
                    {text}
                </Link>
            )
        }, {
            title: formatMessage({
                id: 'tablePanel.column.productCategoryCode',
                defaultMessage: '车型编号'
            }),
            dataIndex: 'productCategoryCode',
            sorter: true
        }, {
            title: formatMessage({
                id: 'tablePanel.column.productCategoryName',
                defaultMessage: '车型名称'
            }),
            dataIndex: 'productCategoryName'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.productCategoryStyle',
                defaultMessage: '车型年款'
            }),
            dataIndex: 'productCategoryStyle'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.status',
                defaultMessage: '状态'
            }),
            dataIndex: 'status',
            render: text => conventEnumValueToString(vehicleStatus, text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.rolloutDate',
                defaultMessage: '下线日期'
            }),
            dataIndex: 'rolloutDate',
            sorter: true,
            render: text => formatDateTime(text, DATETIME_FORMAT)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.productCode',
                defaultMessage: '物料号'
            }),
            dataIndex: 'productCode'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.productName',
                defaultMessage: '物料名称'
            }),
            dataIndex: 'productName'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.productEnglishName',
                defaultMessage: '物料英文名称'
            }),
            dataIndex: 'productEnglishName'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.productGroup',
                defaultMessage: '产品组分类'
            }),
            dataIndex: 'productGroup',
            render: text => conventEnumValueToString(productGroupType, text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.engineModel',
                defaultMessage: '发动机型号'
            }),
            dataIndex: 'engineModel'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.engineCode',
                defaultMessage: '发动机号'
            }),
            dataIndex: 'engineCode'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.transmissionModel',
                defaultMessage: '变速箱型号'
            }),
            dataIndex: 'transmissionModel'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.transmissionSn',
                defaultMessage: '变速箱号'
            }),
            dataIndex: 'transmissionSn'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.engineCylinder',
                defaultMessage: '排量'
            }),
            dataIndex: 'engineCylinder'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.color',
                defaultMessage: '颜色'
            }),
            dataIndex: 'color',
        }, {
            title: formatMessage({
                id: 'tablePanel.column.transmission',
                defaultMessage: '手自动'
            }),
            dataIndex: 'transmission'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.action',
                defaultMessage: '操作'
            }),
            dataIndex: 'action',
            width: FIXED_COLUMN_WIDTH,
            fixed: 'right',
            render: (t, r) => {
                const menus = [
                    {
                        id: PERMISSION.update,
                        children: (
                            <Link key="update" to={routes.update.format(r.id)}>
                                {formatMessage({
                                    id: 'tablePanel.operation.update',
                                    defaultMessage: '编辑'
                                })}
                            </Link>
                        ),
                        hidden: !(hasPermissions(permissions, PERMISSION.update) && hasPermissions(r.options, PERMISSION.update))
                    }
                ];
                return <DropdownMenu key={r.id} menus={menus} primaryLength={1} id={r.id} />;
            }
        }];
        if(brands && brands.length > 1)
            columns.unshift({
                title: formatMessage({
                    id: 'tablePanel.column.brandName',
                    defaultMessage: '品牌'
                }),
                dataIndex: 'brandName',
                sorter: true
            });
        const pagination = {
            total,
            pageSize,
            current: pageIndex + 1,
            onShowSizeChange: this.props.onPageSizeChange,
            onChange: this.props.onPageIndexChange,
            ...PAGINATION_OPTIONS
        };
        const locale = {
            emptyText: this.props.hasError ? COMMON_TABLE_QUERY_FAIL_TEXT : COMMON_TABLE_EMPTY_TEXT
        };
        return (
            <Card>
                <Table
                    className="white-space-nowrap"
                    columns={columns}
                    dataSource={this.props.data}
                    rowKey="id"
                    pagination={pagination}
                    loading={this.props.loading || this.state.loading}
                    onChange={this.onChangeSort}
                    locale={locale}
                    {...TABLE}/>
            </Card>
        );
    }
}

TablePanel.propTypes = {
    data: PropTypes.array.isRequired,
    hasError: PropTypes.string.isRequired,
    initEnumData: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    total: PropTypes.number.isRequired,
    onChangeSort: PropTypes.func.isRequired,
    onPageIndexChange: PropTypes.func.isRequired,
    onPageSizeChange: PropTypes.func.isRequired,
    intl: PropTypes.object,
    pageIndex: PropTypes.number,
    pageSize: PropTypes.number,
};
import {createSelector} from 'reselect';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {tableSearch} from './actions';
const getData = selectorFactory(['page', 'domainData', 'list', 'data']);
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getinitEnumData = createSelector(
    state => state.getIn(['page', 'domainData', 'init']),
    init => {
        const initData = init.toJS();
        const brands = initData.brands ? ListTypeCL(initData.brands) : [];
        return {
            brands, // 品牌
            initData
        };
    }
);

const mapStateToProps = state => ({
    data: getData(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    hasError: state.getIn(['page', 'domainData', 'list', 'hasError']),
    total: state.getIn(['page', 'domainData', 'list', 'total']),
    pageIndex: state.getIn(['page', 'appState', 'queryBySort', 'pageIndex']),
    pageSize: state.getIn(['page', 'appState', 'queryBySort', 'pageSize']),
    permissions: getPermissions(state),
    initEnumData: getinitEnumData(state),
});

const mapDispatchToProps = dispatch => ({
    onPageSizeChange: (pageIndex, pageSize) =>
        dispatch(
            tableSearch({
                pageIndex: PAGE.index,
                pageSize
            })
        ),
    onPageIndexChange: pageIndex =>
        dispatch(
            tableSearch({
                pageIndex: pageIndex - 1
            })
        ),
    onChangeSort: option => dispatch(tableSearch(option)),
});
import {connect} from 'react-redux';
import {injectIntl} from './intl';
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(TablePanel));
